const user =require("../model/user");
const articel=require("../model/article");
const comments=require("../model/comment");
const log =require("../model/log");
const logcom=require("../model/logcoment");
const express=require("express");
const router=express.Router();

router.get("/search",(req,res)=>{
    var reg = new RegExp(req.query.keyword, "g");
    const keyword=req.query.keyword==""?{}:
        {
            $or: [
                { userName: reg }
            ]
    };
    user
        .find(keyword)
        .sort({date:-1})
        .then(result=>{
            res.send({
                code:1,
                message:result
            })
        })
})
router.get("/delete",(req,res)=>{
    const keyword=req.query.keyword;
    user.deleteOne({_id:keyword}).then(result=>{
        comments.deleteMany({user:keyword}).then(com=>{
            logcom.deleteMany({user:keyword}).then(logcom=>{
                if(result.deletedCount==1){
                    res.send({
                        code:1,
                        message:"用户删除成功"
                    })
                }else{
                    res.send({
                        code:0,
                        message:"用户删除失败"
                    })
                }
            })
        })
    })
})
module.exports=router;